package com.yzd.netmonitor.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 字符串处理类
 * @author lipeng
 *
 */
public class StringHelper {

	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.trim().length() == 0 || str.equalsIgnoreCase(null) || str.equalsIgnoreCase("null");
	}

	/**
	 * 判断字符串是否为0
	 * @param str
	 * @return
	 */
	public static boolean isZero(String str) {
		return str.equals("0") || str.equals("0.0");
	}

	public static String delComma(String str) {
		if (!isEmpty(str)) {
			if (str.endsWith(",")) {
				str = str.substring(0, str.length() - 1);
			}
			return str;
		}
		return null;
	}

	/**
	 * 检测字符串是否是int型
	 * @param s
	 * @return
	 */
	public static boolean isInt(String s) {
		try {
			Integer.parseInt(s);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 判断是否为长整型
	 * @param s
	 * @return
	 */
	public static boolean isLong(String s) {
		try {
			Long.parseLong(s);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 字符串转换为字符串数组
	 * 
	 * @param str
	 *            字符串
	 * @param splitRegex
	 *            分隔符
	 * @return
	 */
	public static String[] str2StrArray(String str, String splitRegex) {
		if (isEmpty(str)) {
			return null;
		}
		return str.split(splitRegex);
	}

	/**
	 * 用默认的分隔符(,)将字符串转换为字符串数组
	 * 
	 * @param str
	 *            字符串
	 * @return
	 */
	public static String[] str2StrArray(String str) {
		return str2StrArray(str, ",\\s*");
	}

	/**
	 * 字符串数组转字符串.
	 * @author YRJ
	 *
	 */
	public static String arrayToString(String[] arrays, String regex) {
		StringBuilder tostr = new StringBuilder();
		for (String array : arrays) {
			tostr.append(array).append(regex);
		}
		tostr.deleteCharAt(tostr.length() - 1);
		return tostr.toString();
	}
	
	/**
	 * 字符串数组转字符串.
	 * @author YRJ
	 *
	 */
	public static String listToString(List<String> arrays, String regex) {
		StringBuilder tostr = new StringBuilder();
		for (String array : arrays) {
			tostr.append(array).append(regex);
		}
		tostr.deleteCharAt(tostr.length() - 1);
		return tostr.toString();
	}

	/**
	 * 判断字符串是否包含某字符串
	 * @param s
	 * @param string
	 * @return
	 */
	public static boolean contains(String sourceStr, String indexStr) {
		if (isEmpty(sourceStr)) {
			return false;
		}
		return sourceStr.contains(indexStr);
	}

	/**
	 * 字符串转list
	 * @param str
	 * @param splitRegex
	 * @return
	 */
	public static List<String> strToList(String str, String splitRegex) {
		if (isEmpty(str)) {
			return null;
		}
		final List<String> strList = new ArrayList<String>();
		String[] strArray = str.split("\\" + splitRegex);
		for (String str1 : strArray) {
			strList.add(str1);
		}
		return strList;
	}

	public static <T> String objToStr(T num) {
		if (num == null) {
			return "";
		}
		return String.valueOf(num);
	}

	public static int strToInt(String num) {
		int i = 0;
		try {
			i = Integer.valueOf(num);
		} catch (Exception e) {

		}
		return i;
	}

}